<template>
	<view class="wrap">
		<u-form :model="model" :rules="rules" ref="uForm" :errorType="errorType">
			<u-form-item :label-position="labelPosition" :label-width="150" label="旧密码" prop="oldpassword">
				<u-input :password-icon="true" :border="border" type="password" v-model="model.oldpassword"
					placeholder="请输入旧密码"></u-input>
			</u-form-item>
			<u-form-item :label-position="labelPosition" :label-width="150" label="新密码" prop="password">
				<u-input :password-icon="true" :border="border" type="password" v-model="model.password"
					placeholder="请输入密码"></u-input>
			</u-form-item>
			<u-form-item :label-position="labelPosition" :label-width="150" label="确认密码" label-width="150"
				prop="rePassword">
				<u-input :border="border" type="password" v-model="model.rePassword" placeholder="请确认密码"></u-input>
			</u-form-item>
		</u-form>
		<u-button @click="submit" type="primary">提交</u-button>
	</view>
</template>

<script>
	import {
		getBroker,
		isOnline,
		affirmJieSuan
	} from '@/api/strategy.js'
	import {
		init,
		Traderinit,
		reqQrySecAgentTradingAccount,
		updatePassword
	} from '@/api/trader.js'
	import {
		frequencyInfo
	} from '@/api/member.js'
	var dcRichAlert = uni.requireNativePlugin('Trader')
	export default {
		data() {
			let that = this;
			return {
				model: {
					oldpassword: '',
					password: '',
					rePassword: '',
					remember: false
				},
				rules: {
					password: [{
							required: true,
							message: '请输入密码',
							trigger: ['change', 'blur'],
						},
						{
							// 正则不能含有两边的引号
							pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]+\S{7,12}$/,
							message: '需同时含有字母和数字，长度在8-12之间',
							trigger: ['change', 'blur'],
						}
					],
					oldpassword: [{
							required: true,
							message: '请输入密码',
							trigger: ['change', 'blur'],
						},
						{
							// 正则不能含有两边的引号
							pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]+\S{7,12}$/,
							message: '需同时含有字母和数字，长度在8-12之间',
							trigger: ['change', 'blur'],
						}
					],
					rePassword: [{
							required: true,
							message: '请重新输入密码',
							trigger: ['change', 'blur'],
						},
						{
							validator: (rule, value, callback) => {
								return value === this.model.password;
							},
							message: '两次输入的密码不相等',
							trigger: ['change', 'blur'],
						}
					],
				},
				border: false,
				check: false,
				selectStatus: 'close',
				radio: '支付宝',
				actionSheetShow: false,
				pickerShow: false,
				selectShow: false,
				radioCheckWidth: 'auto',
				radioCheckWrap: false,
				labelPosition: 'left',
				codeTips: '',
				errorType: ['message']
			};
		},
		onLoad() {

		},
		computed: {
			borderCurrent() {
				return this.border ? 0 : 1;
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			test() {
				alert('1')
				uni.navigateTo({
					url: '/pages/user/dealLogin/dealLogin'
				});
			},
			submit() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						uni.setStorageSync('oldPassword', this.model.oldpassword)
						uni.setStorageSync('newPassword', this.model.rePassword)
						updatePassword({
							oldPassword: this.model.oldpassword,
							newPassword: this.model.rePassword,
						}).then(res => {
							uni.showToast({
								title: '重置密码成功,请重新登录',
								icon: 'none',
								duration: 1500
							})
							setTimeout(() => {
								uni.navigateTo({
									url: '/pages/user/dealLogin/dealLogin'
								});
							}, 1500)
						}).catch(err => {
							uni.showToast({
								icon: 'none',
								title: err.message
							})
						})
						// 验证通过
					} else {
						// 验证失败
					}
				});
			},
			// 点击actionSheet回调
			actionSheetCallback(index) {
				uni.hideKeyboard();
				this.model.sex = this.actionSheetList[index].text;
			},
			// checkbox选择发生变化
			checkboxGroupChange(e) {
				this.model.likeFruit = e;
			},
			// radio选择发生变化
			radioGroupChange(e) {
				this.model.payType = e;
			},
			// 勾选版权协议
			checkboxChange(e) {
				this.model.agreement = e.value;
			},
			// 选择地区回调
			regionConfirm(e) {
				this.model.region = e.province.label + '-' + e.city.label + '-' + e.area.label;
			},
			// 选择商品类型回调
			selectConfirm(e) {
				this.model.goodsType = '';
				e.map((val, index) => {
					this.model.goodsType += this.model.goodsType == '' ? val.label : '-' + val.label;
				})
			},
			borderChange(index) {
				this.border = !index;
			},
			radioCheckboxChange(index) {
				if (index == 0) {
					this.radioCheckWrap = false;
					this.radioCheckWidth = 'auto';
				} else if (index == 1) {
					this.radioCheckWrap = true;
					this.radioCheckWidth = 'auto';
				} else if (index == 2) {
					this.radioCheckWrap = false;
					this.radioCheckWidth = '50%';
				}
			},
			labelPositionChange(index) {
				this.labelPosition = index == 0 ? 'left' : 'top';
			},
			codeChange(text) {
				this.codeTips = text;
			},
			// 获取验证码
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码',
						mask: true
					})
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						this.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 2000);
				} else {
					this.$u.toast('倒计时结束后再发送');
				}
			},
			errorChange(index) {
				if (index == 0) this.errorType = ['message'];
				if (index == 1) this.errorType = ['toast'];
				if (index == 2) this.errorType = ['border-bottom'];
				if (index == 3) this.errorType = ['border'];
			}
		}
	};
</script>

<style scoped lang="scss">
	.wrap {
		padding: 30rpx;
	}

	.agreement {
		display: flex;
		align-items: center;
		margin: 40rpx 0;

		.agreement-text {
			padding-left: 8rpx;
			color: $u-tips-color;
		}
	}
</style>
